<?php	
	include('db_connection.php');
		if(isset($_POST['pay_type_status'])){
			$return_type = '';
			$select_usr_dtl = "SELECT * FROM tbl_user_details WHERE id='".$_POST['userdtlpayerId']."'";
			$query_usr_dtl = mysql_query($select_usr_dtl) or die(mysql_error());

			if(mysql_num_rows($query_usr_dtl)>0){
				while($row_usr_dtl = mysql_fetch_array($query_usr_dtl)){
					$course_id = $row_usr_dtl['course_id'];
					$course_fee = '';
					$course_pwd = '';

					if($row_usr_dtl['is_ebook'] == 1){
						$sel_course_fee = "SELECT * FROM tbl_ebook where id = '".$course_id."'";
						$query_course_fee = mysql_query($sel_course_fee) or die(mysql_error());
						if(mysql_num_rows($query_usr_dtl)>0){
							while($row_course_fee = mysql_fetch_array($query_course_fee)){
								$course_fee = $row_course_fee['ebook_fee'];
								$course_pwd = $row_course_fee['ebook_pwd'];
							}
						}
					}else{
						$sel_course_fee = "SELECT * FROM tbl_courses where id = '".$course_id."'";
						$query_course_fee = mysql_query($sel_course_fee) or die(mysql_error());
						if(mysql_num_rows($query_usr_dtl)>0){
							while($row_course_fee = mysql_fetch_array($query_course_fee)){
								$course_fee = $row_course_fee['course_fee'];
							}
						}
					}
					
					$user_id = $row_usr_dtl['user_id'];
					$insert_cart = "INSERT INTO tbl_cart_payment (user_id,course_id,user_detail_id,payment) values('".$row_usr_dtl['user_id']."','".$course_id."','".$_POST['userdtlpayerId']."','".$course_fee."')";
					$query_insert_cart = mysql_query($insert_cart) or die(mysql_error());

					$sel_bank_status = "SELECT * FROM tbl_bank_status WHERE id='".$_POST['pay_type_status']."'";
					$course_bank_status = mysql_query($sel_bank_status) or die(mysql_error());
					if(mysql_num_rows($course_bank_status)>0){
						while($row_bank_status = mysql_fetch_array($course_bank_status)){
							$return_type[] = $row_bank_status['type'];
							$update_bak_status  = "UPDATE tbl_user_details SET via_paid='".$row_bank_status['type']."' WHERE id='".$_POST['userdtlpayerId']."'";
							$query_bank_status = mysql_query($update_bak_status) or die(mysql_error());
						}
					}

					$update = "UPDATE tbl_user SET is_paid='1', is_active='1', update_ts=now() WHERE id='".$row_usr_dtl['user_id']."'";
					$user_update = mysql_query($update) or die(mysql_error());
					$msg = "Bank Detail Updated Successfully";

					$select_user = "SELECT * FROM tbl_user WHERE id='".$row_usr_dtl['user_id']."'";		
					$query_user = mysql_query($select_user) or die(mysql_error());
					$count_row_user = mysql_num_rows($query_user);
					$email = '';
					
					if($count_row_user>0){
						while($row = mysql_fetch_array($query_user)){
							$email = $row['email'];
							$refrence_url = $row['reference_url'];
						}
					}

					if($row_usr_dtl['is_ebook']){
						$mail_to=$email ;
						$mail_subject="Download Ebook Link";
						$mail_body.="Click the following link to download your ebook.<br/>\n";
						$mail_body.="Password = ".$course_pwd.".<br/>\n";
						$mail_body.="<a href='http://rvtraining.net/download_ebook.php?download_link=".$refrence_url.".".$row_usr_dtl['id']."'>Click here</a>";
						$headers  = 'MIME-Version: 1.0' . "\r\n";
						$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
						$headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
						$sent = mail($mail_to,$mail_subject,$mail_body, $headers);
					}
					echo json_encode($return_type);

				}
			}

		}
?>